const page = new buildPage({
    dom: '#buildPage',
    data: {
        page: {
            title: '登录',
            type: 'login'
        },
        formData: {
            userInfo: {
                userName: '',
                passWord: '',
                confirmPassword: '',
                email: '',
                sms: {
                    code: '',
                    uuid: ''
                }
            }
        }
    },
    // 初始化后回调
    created () {
        if (Cookies.get('is_login')) {
            layer.msg('你已登录')
            window.location.href = `${window.location.origin}/cms/home/cms-user-home`
        }
    },
    // 方法集合
    methods: {
        click() {
            if (this.data.formData.userInfo.userName === '') {
                layer.msg('用户名不能为空')
                return
            }
            // 登录
            if (this.data.page.type === 'login') {
                return login(this.data.formData.userInfo)
            }
            // 注册
            if (this.data.page.type === 'register') {

                if (this.data.formData.userInfo.email === '') {
                    layer.msg('邮箱不能为空')
                    return
                }

                if (this.data.formData.userInfo.sms.uuid === '') {
                    layer.msg('请先获取注册码')
                    return
                }

                userRegister(this.data.formData.userInfo).then(res => {
                    layer.msg(res.msg)
                    if (res.code === 200) {
                        let obj = {
                            type: this.data.page.type === 'login' ? 'register' : 'login',
                            title: this.data.page.type === 'login' ? '注册' : '登录'
                        }
                        Reflect.set(this.data, 'page', obj)
                    }
                })
            }
        },
        showWechat() {
            layer.open({
                title: '请用微信扫一扫后登录'
                ,content: '<div id="qrcode"></div>'
                ,success: function(layero, index){
                    getWeChatCommunityAuthUrl({
                        config_id: 2
                    }).then(res => {
                        if (res.code !== 302) {
                            layer.msg('获取授权凭证失败')
                            return
                        }
                        let qrcode = new QRCode(document.getElementById("qrcode"), {
                            width : 200,
                            height : 180,
                            margin: '0 auto'
                        })
                        qrcode.makeCode(res.items.url);
                    })
                }
            });
        },
        changePage() {
            let obj = {
                type: this.data.page.type === 'login' ? 'register' : 'login',
                title: this.data.page.type === 'login' ? '注册' : '登录'
            }
            Reflect.set(this.data, 'page', obj)
            Reflect.set(this.data.formData.userInfo.sms, 'uuid:noUpdate', '')
        },
        // 获取注册码
        getRegisterCode() {

            if (!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(this.data.formData.userInfo.email)) {
                layer.msg('邮箱格式不正确')
                return
            }

            if (this.data.formData.userInfo.sms.uuid === '') {
                sendSms({
                    to: this.data.formData.userInfo.email,
                    sms_service: 'send_mail',
                    sms_model: 'register'
                }).then(res => {
                    layer.msg(res.msg)
                    if (res.code === 200) {
                        Reflect.set(this.data.formData.userInfo.sms, 'uuid:noUpdate', res.items.uuid)
                    }
                })
                return
            }
            layer.msg('注册码已发送')
        },
        // INPUT 输入事件
        inputUserNme(e) {
            Reflect.set(this.data.formData.userInfo, 'userName:noUpdate', e.value)
        },
        inputPassword(e) {
            Reflect.set(this.data.formData.userInfo, 'passWord:noUpdate', e.value)
        },
        inputRegisterCode(e) {
            Reflect.set(this.data.formData.userInfo.sms, 'code:noUpdate', e.value)
        },
        inputConfirmPassword(e) {
            Reflect.set(this.data.formData.userInfo, 'confirmPassword:noUpdate', e.value)
        },
        inputEmail(e) {
            Reflect.set(this.data.formData.userInfo, 'email:noUpdate', e.value)
        }
    }
}).mount()
